package com.mybatis.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mybatis.Dao.IMemberMapper;
import com.mybatis.Util.MyBatisUtil;
import com.mybatis.Util.Result;
import com.mybatis.entity.Employee;

/**
 * Servlet implementation class empServlet
 */
@WebServlet("/empServlet")
public class empServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public empServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");//设置输出信息编码方式为utf-8
			response.setContentType("text/json");//设置输出信息格式
			
			int page = Integer.parseInt(request.getParameter("page"));//获取当前页面page值
			int limit = Integer.parseInt(request.getParameter("limit"));//获取limit
			
					
			String key = request.getParameter("key");//获取模糊查询的姓名
			String sex = request.getParameter("sex");//获取模糊查询的性别
			String dep = request.getParameter("dep");//获取模糊查询的部门名称
			String bir = request.getParameter("bir");//获取模糊查询的生日
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("name",key);
			map.put("sex",sex);
			map.put("birthday",bir);
			map.put("dptName",dep);
			map.put("page",(page-1)*limit);
			map.put("limit",limit);
			
			//调用模糊查询方法
			IMemberMapper mdao = MyBatisUtil.getMapper(IMemberMapper.class);
			List<Employee> list = mdao.queryBykey(map);
			int count = mdao.getCount(map);
			//将执行结果存入结果集
			Result rs = new Result(0,"成功",count,list);
			//将结果集转为json格式
			Gson g= new Gson();
			String json = g.toJson(rs);
			//输出结果集
			PrintWriter out =response.getWriter();
			out.print(json);
			//释放资源
			out.close();
			MyBatisUtil.closeSqlSession();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
